<template>
  <!-- 预约列表 -->
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getTableData()">
      <el-form-item label="宠物主人">
        <el-input v-model="dataForm.nickName" placeholder="宠物主人" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="getTableData()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()" v-if="roleList[0].roleId != '3'">新增预约</el-button>
        <el-button type="danger" @click="onDelete()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      border
      :data="tableData"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" width="50" header-align="center" align="center"></el-table-column>
      <el-table-column prop="nickName" label="宠物主人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="petName" label="宠物名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="phone" label="预约手机号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="medicName" label="预约医生" header-align="center" align="center"></el-table-column>
      <el-table-column prop="time" label="预约时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="prescription" label="用药&器械" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" label="预约状态" header-align="center" align="center">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status ? 'success' : 'danger'"
          >{{ scope.row.status ? '已结束' : '未开始' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="250" label="操作" header-align="center" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="addOrUpdateHandle(scope.row.id)"
            :disabled="!!scope.row.status"
            size="mini"
          >修改预约</el-button>
          <el-button
            type="primary"
            @click="applyFinish(scope.row.id)"
            size="mini"
            v-if="roleList[0].roleId == '1' || roleList[0].roleId == '3'"
          >诊断</el-button>
          <el-button
            type="danger"
            @click="onDelete(scope.row.id)"
            size="mini"
            :disabled="!!scope.row.status"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getTableData"></add-or-update>
    <measure-dialog v-if="measureDialogVisible" ref="MeasureDialog" @refreshDataList="getTableData"></measure-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update.vue";
import MeasureDialog from "./measure-dialog";
import { getDataList, delDataList } from "@/api/apply";

import { mapGetters } from "vuex";
export default {
  components: { AddOrUpdate, MeasureDialog },
  data() {
    return {
      dataForm: {},
      tableData: [],
      dataListLoading: false,
      dataListSelections: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addOrUpdateVisible: false,
      measureDialogVisible: false,
 
    };
  },
  computed: {
    ...mapGetters(["userInfo", "roleList"])
  },
  created() {
    this.getTableData();
  },
  methods: {

    isOver(prop) {
      let current = new Date().getTime(),
        time = new Date(prop).getTime();
      return current >= time;
    },
    // 获取表格数据
    getTableData() {
      getDataList({
        ...this.dataForm,
        userId: this.roleList[0].roleId == "2" ? this.userInfo.id : "",
        careId: this.roleList[0].roleId == "3" ? this.userInfo.id : "",
        limit: this.pageSize,
        page: this.pageIndex
      })
        .then(res => {
          if (res && res.code === 0) {
            this.tableData = res.page.list;
            this.totalPage = res.page.totalCount;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 编辑
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    applyFinish(id) {
      this.measureDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.MeasureDialog.init(id);
      });
    },
    // 删除
    onDelete(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delDataList(ids)
          .then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getTableData();
                }
              });
            }
          })
          .catch(err => {
            console.log(err);
          });
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getTableData();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getTableData();
    }
  }
};
</script>

<style scoped>
.search-btn {
  margin-bottom: 10px;
}
</style>